// Package logger provides a rotating log system for the WePayKit application.
// This file contains the global logger instance and initialization functions.
package logger

import (
	"fmt"
	"sync"
)

var (
	// Global logger instance
	globalLogger *Logger
	// Mutex to ensure thread-safe initialization
	once sync.Once
)

// Init initializes the global logger instance
func Init() error {
	var initErr error
	
	once.Do(func() {
		var err error
		globalLogger, err = New("./logs", "wpk")
		if err != nil {
			initErr = fmt.Errorf("failed to initialize logger: %w", err)
		}
	})
	
	return initErr
}

// Get returns the global logger instance
func Get() *Logger {
	return globalLogger
}

// Info logs an info level message using the global logger
func Info(message string) {
	if globalLogger != nil {
		globalLogger.Info(message)
	}
}

// Error logs an error level message using the global logger
func Error(message string) {
	if globalLogger != nil {
		globalLogger.Error(message)
	}
}

// Warn logs a warning level message using the global logger
func Warn(message string) {
	if globalLogger != nil {
		globalLogger.Warn(message)
	}
}

// Debug logs a debug level message using the global logger
func Debug(message string) {
	if globalLogger != nil {
		globalLogger.Debug(message)
	}
}

// Sync flushes the global logger's data to disk
func Sync() {
	if globalLogger != nil {
		// The logger automatically syncs after each write, so this is a no-op
		// We keep it for compatibility with common logging patterns
	}
}